/**
 * @file codeforces/1528/a/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/6/22 10:56:49
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 4;
ll D[N][2], L[N][2];
vector<int> G[N];
void dfs(int u, int fa = -1) {
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs(v, u);
    for (int b = 0; b < 2; ++b) {
      D[u][b] += max(abs(L[u][b] - L[v][1]) + D[v][1],
                     abs(L[u][b] - L[v][0]) + D[v][0]);
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int n, t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> L[i][0] >> L[i][1];
      G[i].clear();
      D[i][0] = D[i][1] = 0;
    }
    for (int i = 1, u, v; i < n; ++i) {
      cin >> u >> v;
      G[u].push_back(v);
      G[v].push_back(u);
    }
    dfs(1);
    cout << max(D[1][0], D[1][1]) << '\n';
  }
}
